<?php include 'header.php' ?>

    <div class="container_12 primary_content_wrap clearfix">
        <div class="grid_8 right" id="content" style="width: 920px;">
            <div class="header-title">
                <h1 class="page_title">Tư vấn - Tin tức</h1>
            </div>

            <?php
            include 'mySQLConnect.php';

            //xac dinh bao nhieu dong
            $display = 5;
            // tinh tong so trang can hien thi
            if (isset($_GET['page']) && (int)$_GET['page']) {
                $page = $_GET['page'];

            } else {
                //neu chua xac dinh, thi tim so trang
                $query = "SELECT COUNT(id) FROM news";
                $res = mysql_query($query);
                $rows = mysql_fetch_array($res) or die(mysql_error());
                $record = $rows[0];
                if ($record > $display) {
                    $page = ceil($record / $display);
                } else {
                    $page = 1;
                }
            }

            $start = (isset($_GET['start']) && (int)$_GET['start'] >= 0) ? $_GET['start'] : 0;

            $sql3 = "SELECT * FROM  news ORDER BY date DESC LIMIT $start, $display";

            $result3 = mysql_query($sql3);
            $count = 0;
            while ($row3 = mysql_fetch_array($result3)) {
                $count++;
                $image_url = $row3['image_url'];

                if (strpos($image_url, '../') !== false) {
                    $image_url = substr($image_url, 3);
                }

                $date = date("d M", time($row3['date']));

                ?>
                <article
                    class="post-77 post type-post status-publish format-standard hentry category-adipisicing-eliters-sed-do-eiusmod tag-augue-quis tag-bibendum-mauris tag-elit tag-ipsum-dolor tag-tempor post-holder cat-7-id"
                    id="post-77">
                    <header class="entry-header">
                        <time><?php echo $date ?></time>
                        <div class="post-head-right">
                            <h2 class="entry-title">
                                <a title=""
                                   href="detailNews.php?id=<?php echo $row3['id'] ?>"><?php echo urldecode($row3['name']) ?></a>
                            </h2>

                            <div class="post-meta">
                                <div class="fleft">đăng bởi mobile shop
                                </div>
                            </div>
                        </div>
                    </header>
                    <figure class="featured-thumbnail"><a
                            href="detailNews.php?id=<?php echo $row3['id'] ?>"><img
                                width="230" height="155" alt="Flower shop"
                                class="attachment-post-thumbnail wp-post-image"
                                src="<?php echo $image_url ?>"></a>
                    </figure>
                    <div class="post-content not-single">
                        <div class="excerpt">
                            <?php echo urldecode($row3['short_desc']) ?>
                        </div>
                        <a class="button"
                           href="detailNews.php?id=<?php echo $row3['id'] ?>">Chi tiết</a>
                    </div>
                    <div class="clear"></div>
                </article>

            <?php
            }

            include 'mySQLClose.php';
            ?>

            <div class="pagenavi">
                <?php
                if ($page > 1) { //neu can hien thi so trang
                    $next = $start + $display;
                    $prev = $start - $display;
                    $current = ($start / $display) + 1;

                    $datastrPrv = 'start=' . $prev . '&page=' . $page;
                    $datastrNext = "start=" . $next . "&page=" . $page;

                    //Hien thi trang Previous
                    if ($current != 1) {
                        ?>
                        <a class="pagenavi_prev" href="news.php?<?php echo $datastrPrv; ?>">quay về</a>
                    <?php
                    }
                    //Hien thi so link
                    for ($i = 1; $i <= $page; $i++) {
                        if ($current != $i) {
                            $datastrPage = "start=" . ($display * ($i - 1)) . "&page=" . $page;
                            ?>
                            <a class="inactive" href="news.php?<?php echo $datastrPage; ?>"><?php echo $i ?></a>
                        <?php
                        } else {
                            echo "<span class='current'>$i</span>";
                        }
                    } //End: FOR

                    //Hien thi trang Next

                    if ($current != $page) {
                        ?>
                        <a class="pagenavi_next" href="news.php?<?php echo $datastrNext ?>">tiếp theo</a>
                    <?php
                    }
                }
                ?>

            </div>

        </div>
    </div>

<?php include 'footer.php' ?>